<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <title>newproject</title>
    <meta name="description" content="">
    <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
    <meta name="robots" content="all,follow">
    <!-- Bootstrap CSS-->
    <link rel="stylesheet" href="vendor/bootstrap/css/bootstrap.min.css">
    <!-- Font Awesome CSS-->
    <link rel="stylesheet" href="vendor/font-awesome/css/font-awesome.min.css">
    <!-- Fontastic Custom icon font-->
    <link rel="stylesheet" href="css/fontastic.css">
    <!-- Google fonts - Poppins -->
    <link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Poppins:300,400,700">
    <!-- theme stylesheet-->
    <link rel="stylesheet" href="css/style.default.css" id="theme-stylesheet">
    <!-- Custom stylesheet - for your changes-->
    <link rel="stylesheet" href="css/custom.css">
    <!-- Favicon-->
    <link rel="shortcut icon" href="img/favicon.ico">
    <!-- Tweaks for older IEs--><!--[if lt IE 9]>
        <script src="https://oss.maxcdn.com/html5shiv/3.7.3/html5shiv.min.js"></script>
        <script src="https://oss.maxcdn.com/respond/1.4.2/respond.min.js"></script><![endif]-->
  </head>
  <body>
    <div class="page">
     
        
      <div class="page-content d-flex align-items-stretch"> 
        <!-- Side Navbar -->
       
        <div class="content-inner" style="width: 100%;">
          <!-- Page Header-->
         
          <!-- Forms Section-->
          <section class="forms no-padding-top"> 
            <div class="container-fluid">
              <div class="row">
              
                <!-- Form Elements -->
                <div class="col-lg-12" >
                  <div class="card" >
                    <div class="card-close">
                      <div class="dropdown">
                        <button type="button" id="closeCard5" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false" class="dropdown-toggle"><i class="fa fa-ellipsis-v"></i></button>
                        <div aria-labelledby="closeCard5" class="dropdown-menu dropdown-menu-right has-shadow"><a href="#" class="dropdown-item remove"> <i class="fa fa-times"></i>Close</a><a href="#" class="dropdown-item edit"> <i class="fa fa-gear"></i>Edit</a></div>
                      </div>
                    </div>
                    <div class="card-header d-flex align-items-center">
                      <h3 class="h4">新建项目</h3>
                    </div>
                    <div class="card-body">
                      <form class="form-horizontal">
                        <div class="form-group row no-padding-bottom">
                          <label class="col-sm-3 form-control-label">项目名称</label>
                          <div class="col-sm-9">
                            <input type="text" name="proejctName" id="projectName" class="form-control">
                          </div>
                        </div>
                        <div class="form-group row no-padding-bottom">
                          <label class="col-sm-3 form-control-label">项目类型</label>
                          <div class="col-sm-9">
                            <select name="isremoved" id="isremoved" class="form-control mb-3">
                              <option value="0">普通</option>
                              <option value="1">拆迁</option>
                              
                            </select>
                          </div>
                        </div>
                        <div class="form-group row no-padding-bottom">
                          <label class="col-sm-3 form-control-label">项目经理</label>
                          <div class="col-sm-9">
                            <select name="manager" id="manager" class="form-control mb-3">
                             
                            </select>
                          </div>
                        </div>
                        <div class="line no-margin-top"></div>
                        <div class="form-group row no-padding-bottom">
                          <label class="col-sm-3 form-control-label">委托方</label>
                          <div class="col-sm-9">
                            <input type="text" name="customer" id="customer" class="form-control">
                          </div>
                        </div>
                        <div class="form-group row" style="height: auto;">
                          <label class="col-sm-3 form-control-label">估价目的</label>
                          <div class="col-sm-9" style="height: auto;">
                           
                            <textarea class="form-control" rows="5" id="valuationPurpose" ></textarea>
                            <small class="help-block-none">可拖拽右下角调整输入框大小.</small>
                          </div>
                        </div>
                        <div class="line"></div>
                        
                        <div class="form-group row">
                          <label class="col-sm-3 form-control-label h4">估值物</label>
                          <div class="col-sm-2 float-right " style="margin-right: 0px;">
                          	<a id="addValuationBtn" class="btn btn-primary col-5 bg-gray " style="margin-right: 0px;">+</a>
                          	<a id="removeValuationBtn" class="btn btn-primary col-5 bg-gray " style="margin-right: 0px;">-</a>
                          </div>
                        </div>
                        <div class="line no-margin-top"></div>
                        
                        
                        <div id="valuationBox">
	                        	<!-- 估值物信息-->
	                        	
		                        <!-- 估值物信息-->
		                        
	                        <div class="form-group row Valuations">
	                          
	                          <div class="col-lg-2">
	                            <select   class="form-control mb-3">
	                              <option value="1">房产</option>
	                              <option value="0">土地</option>
	                              
	                            </select>
	                          </div>
	                          <div class="col-sm-8">
	                            <input type="text"  placeholder="估值物描述" class="form-control">
	                          </div>
	                          
	                        </div>
	                        
                        </div>
                        <div class="line"></div>
                        
                        
                      
                        <div class="line"></div>
                        <div class="form-group row">
                          <div class="col-sm-4 offset-sm-3">
                            
                            <label id="crateProjectBtn"  class="btn btn-primary">创建</label>
                          </div>
                        </div>
                      </form>
                    </div>
                  </div>
                </div>
              </div>
            </div>
          </section>
         
        </div>
      </div>
    </div>
    <!-- JavaScript files-->
    <script src="vendor/jquery/jquery.min.js"></script>
    <script src="vendor/popper.js/umd/popper.min.js"> </script>
    <script src="vendor/bootstrap/js/bootstrap.min.js"></script>
    <script src="vendor/jquery.cookie/jquery.cookie.js"> </script>
    <script src="vendor/chart.js/Chart.min.js"></script>
    <script src="vendor/jquery-validation/jquery.validate.min.js"></script>
    <!-- Main File-->
    <script src="js/front.js"></script>
    <script type="text/javascript">
    	var valuationInfo = '<div class="form-group row Valuations">'+
															'<div class="col-lg-2">'+
	 															' <select   class="form-control mb-3" value="0">'+
													  			'<option value="1">房产</option>'+
													    	 ' <option value="2">土地</option>'+
													    	 
													      ' </select>'+
												      ' </div> <div class="col-sm-8">'+
												       ' <input type="text" placeholder="估值物描述" class="form-control">'+
												       ' </div></div>';
    	$("#addValuationBtn").bind("click",function(){
    		$("#valuationBox").append(valuationInfo);
    	});
    	$("#removeValuationBtn").bind("click",function(){
    		if( 0 != $("#valuationBox").children().last().index()){
    			$("#valuationBox").children().last().remove();
    		}else{
    			alert("项目中待估价物不能为零！");
    		}
    			
    	});
    	var projectName = "";
    	var projectManager = "";
    	var isremoved = "";
    	var customer = "";
    	var valuationPurpose = "";
    	var valuations = [];
    	var checkFlag  = true;
    	
    	//获取项目经理
    	getManager();
    	
    	
    	$("#crateProjectBtn").bind("click",function(){
    		projectName = $("#projectName").val();
    		isremoved = $("#isremoved").val();
    		projectManager = $("#manager").val();
    		customer = $("#customer").val();
    		valuationPurpose = $("#valuationPurpose").val();
				var valuationTypes = [];
				
				var valuationTexts = [];	
				var land = [];
				var building = [];
				if(projectName ==""){
					alert("项目名称不能为空！");
					$("#projectName").focus();
				}
				console.log(projectManager);
				if(projectManager ==""){
					alert("请指定项目经理！");
					$("#manager").focus();
				}
				if(customer ==""){
					alert("请输入委托方信息！");
					$("#customer").focus();
				}
				if(valuationPurpose ==""){
					alert("请输入估价目的！");
					$("#valuationPurpose").focus();
				}
				
				$("#valuationBox").find("select").each(function(i,n){
					valuationTypes.push($(n).val());
				});
				$("#valuationBox").find("input").each(function(i,n){
					if($(n).val() == ""){
						alert("描述为必填数据，请核查！");
						valuationTypes = [];
						valuationTexts = [];
						checkFlag = false;
						return 0;
					}
					valuationTexts.push($(n).val());
				});
    		
    		for(var i=0; i<=valuationTypes.length;i++){
    			
    			if(valuationTypes[i]==1){
    				building.push(new Building(valuationTexts[i]));
    			}
    			if(valuationTypes[i]==2){
    				land.push(new Land(valuationTexts[i]));
    			}
    			
    		}
    		
    		if(checkFlag == true){
    			
    			
    			 $.ajax({
              type:"post",
              url:"/business.spr?method=createProject",
              contentType:"application/json",
              dataType: "json",
              data:JSON.stringify({
                  'projectname':projectName,
                  'isremoved':isremoved,
                  'manager':projectManager,
                  'customer':customer,
                  'purpose':valuationPurpose,
                  'land':land,
                  'building':building
             }),
              success:function(data){
                  var dataObj=eval("("+data+")");

                  if(dataObj.result!="0000"){
                      alert(dataObj.msg);
                  }else{
                  	var projectid = dataObj.params.projectId;
                      alert("项目"+projectid+"创建成功 ");
                     
                  $(".card").find("input").attr("disabled","true");
		              $(".card").find("select").attr("disabled","true");
		              $("#valuationPurpose").attr("disabled","true");
		              $("a[id$='Btn']").remove();
		              $("#crateProjectBtn").html("查看");
		              $("#crateProjectBtn").unbind("click");
		              $("#crateProjectBtn").bind("click",function(){
		              	parent.addTabs("查看项目","view_project.html?projectId="+projectid,1,null);
		              });
		              
                  }
              },
              error:function(){
              	
              	 
                  alert("提交失败！");

              }
          });


    		}
    		checkFlag = true;
    		
    	});
    	
    	
    	function getManager(){
    		
    		$.ajax({
    			type:"get",
    			url:"/login.spr?method=getManager",
    			contentType:"application/json",
          dataType: "json",
          data:{},
          success:function(data){
          	var dataObj=eval("("+data+")");

                  if(dataObj.result!="0000"){
                      alert(dataObj.msg);
                  }else{
                  	var opttemp = "";
                  	for(var i =0;i< dataObj.params.manager.length;i++){
                  		opttemp = opttemp + '<option value="'+dataObj.params.manager[i].id+'">'+dataObj.params.manager[i].nickname+'</option>'
                  	}
                  	
                  	$("#manager").append(opttemp);
                  }
          	
          	
          }
    		});
    	}
    	
    	
    	//创建 估价对象
    	function valuationObj(valuationType,valuationdetail){	
    		this.valuationType = valuationType;
    		this.valuationdetail = valuationdetail;
    	}
    	
    	function Land(position){
    		this.position = position;
    	}
    	
    	function Building(position){
    		this.position = position;
    	}
    </script>
  </body>
</html>
